//
//  NovelThreeTableViewCell.m
//  novel
//
//  Created by 高青松 on 2018/8/11.
//  Copyright © 2018年 Tracy. All rights reserved.
//

#import "NovelThreeTableViewCell.h"
#import "NovelThreeCollectionViewCell.h"
#import "NovelCollectionReusableView.h"
#import "novelThirdModel.h"
@interface NovelThreeTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>


@end
@implementation NovelThreeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addSubview:self.collectionView];
    }
    return self;
}
- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    CGFloat threeCellH = 0;
    for (NSDictionary * dataDic in dataArray) {
        NSArray * datasouceArray = [dataDic objectForKey:@"dataSource"];
        threeCellH += (datasouceArray.count/3 + (datasouceArray.count % 3 > 0 ? 1 : 0 )) * 160 * kProportionW;
    }
    _collectionView.frame = CGRectMake(10 * kProportionW, 0, kScreenW - 20 * kProportionW,threeCellH + 50 * kProportionW * dataArray.count);
    _collectionView.contentSize = CGSizeMake(kScreenW - 20 * kProportionW, threeCellH + 50 * kProportionW * dataArray.count);
}

#pragma mark --collectionView--
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10 * kProportionW, 0, kScreenW - 20 * kProportionW,1000 * kProportionW) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate=self;
        _collectionView.dataSource=self;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = FALSE;
        _collectionView.showsHorizontalScrollIndicator = FALSE;
        [_collectionView registerClass:[NovelThreeCollectionViewCell class] forCellWithReuseIdentifier:@"NovelThreeCollectionViewCell"];
        [_collectionView registerClass:[NovelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NovelCollectionReusableView"];
        
        
    }
    return _collectionView;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenW, 50*kProportionW);
}

//这个也是最重要的方法 获取Header的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        NovelCollectionReusableView * header = (NovelCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([NovelCollectionReusableView class]) forIndexPath:indexPath];
        NSDictionary * dataDic = self.dataArray[indexPath.section];
        [header.titleLabel setText:[dataDic objectForKey:@"section"]];
        return header;
        
    }
    
    return nil;
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray * dataSourceArray = [self.dataArray[section] objectForKey:@"dataSource"];
    return dataSourceArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NovelThreeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NovelThreeCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSDictionary * dataSourceDic = self.dataArray[indexPath.section];
    NSArray * detailArray = [dataSourceDic objectForKey:@"dataSource"];
    novelThirdModel * model = detailArray[indexPath.row];
    [cell.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    [cell.titleLabel setText:model.name];
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0F;
}


-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.1F;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenW - 20.1 * kProportionW)/3, 160 * kProportionW);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout *)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"--section=%ld--row=%ld----",indexPath.section,indexPath.row);
}


@end
